package com.lottery.im.module.chat.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.ShowTipObserver;
import com.lottery.im.api.result.entity.ReceivedRedPacketsReturnMap;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.chat.view.RedPacketsDetailView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;

public class RedPacketsDetailPresenter extends BaseFragmentPresenter<RedPacketsDetailView> {

  public RedPacketsDetailPresenter(Activity activity,
      RedPacketsDetailView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  /**
   * 查看单个红包详情
   *
   * @param id 红包ID
   */
  public void querySingleRedPacketsDetail(String id) {
    ApiClient.getApiRetrofitInstance()
        .querySingleRedPacketsDetail(MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken(), id)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new ShowTipObserver<ReceivedRedPacketsReturnMap>(activity) {
          @Override public void onSuccess(ReceivedRedPacketsReturnMap receivedRedPacketsReturnMap) {
            iView.queryRedPacketsDetailSuce(receivedRedPacketsReturnMap);
          }
        });
  }

  /**
   * 查看群红包详情
   *
   * @param id 红包ID
   */
  public void queryRedPacketsDetail(String id) {
    ApiClient.getApiRetrofitInstance()
        .queryRedPacketsDetail(MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken(), id)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new ShowTipObserver<ReceivedRedPacketsReturnMap>(activity) {
          @Override public void onSuccess(ReceivedRedPacketsReturnMap receivedRedPacketsReturnMap) {
            iView.queryRedPacketsDetailSuce(receivedRedPacketsReturnMap);
          }
        });
  }
}
